package com.yw.common.core.util;

import java.util.Map;
import java.util.Set;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.GetMethod;

import org.apache.commons.httpclient.methods.PostMethod;

import com.yw.common.http.entity.HttpRequestTypeEnum;
import com.yw.common.http.entity.HttpTextResponse;
import com.yw.common.http.entity.IHttpResponse;

/**
 * http工具
 * @author aoshiguchen
 * @time 2015-09-23
 */

public class HttpUtil {
	public static String getUrlStringByBaseUrlAndParams(String baseUrl,Map<String,String> params){
		StringBuffer sb = new StringBuffer();
		
		sb.append(baseUrl);
		
		if(params!=null && params.size() > 0){
			sb.append("?");
			sb.append(params2String(params));
		}
		
		return sb.toString();
	}
	
	public static String params2String(Map<String,String> params){
		StringBuffer sb = new StringBuffer();
		Set<String> keySet = params.keySet();
		
		if(params!=null && params.size() > 0){
			for(String key : keySet){
				sb.append(key);
				sb.append("=");
				sb.append(params.get(key));
				sb.append("&");
			}
			sb.deleteCharAt(sb.length() - 1);
		}
		
		return sb.toString();
	}
	
	public static IHttpResponse sendGetRequest(String url,Map<String,String> params,String charSet) throws Exception{
		IHttpResponse httpResponse = new HttpTextResponse();
		
		String fullUrl = getUrlStringByBaseUrlAndParams(url,params);
		
		HttpClient client = new HttpClient();
		GetMethod getMethod = new GetMethod(fullUrl);
		
		int status = client.executeMethod(getMethod);
		String content = IOUtil.inputStreamToString(getMethod.getResponseBodyAsStream(),charSet);
		
		httpResponse.setStatus(status);
		httpResponse.setContent(content);
		
		return httpResponse;
	}
	
	public static IHttpResponse sendGetRequest(String url) throws Exception{
		
		return sendGetRequest(url,null,"utf-8");
	}
	
	public static IHttpResponse sendGetRequest(String url,String charSet) throws Exception{
		
		return sendGetRequest(url,null,charSet);
	}
	
	public static IHttpResponse sendPostRequest(String url,Map<String,String> params) throws Exception{
		IHttpResponse httpResponse =  new HttpTextResponse();
		
		HttpClient client = new HttpClient();
		PostMethod postMethod = new PostMethod(url);
		
		for(String key:params.keySet()){
			postMethod.addParameter(key,params.get(key));
		} 
		
		int status = client.executeMethod(postMethod);
		String content = postMethod.getResponseBodyAsString();
		
		httpResponse.setStatus(status);
		httpResponse.setContent(content);
		
		return httpResponse;
	}
	
	
	
	public static IHttpResponse sendPostRequest(String url) throws Exception{
		
		return sendPostRequest(url,null);
	}
	
	public static IHttpResponse sendHttpRequest(HttpRequestTypeEnum method,String url,Map<String,String> params,String charSet) throws Exception{
		IHttpResponse httpResponse = null;

		if(method == HttpRequestTypeEnum.GET){
			httpResponse = sendGetRequest(url,params,charSet);
		}else if(method == HttpRequestTypeEnum.POST){
			httpResponse = sendPostRequest(url,params);
		}
		
		return httpResponse;
	}
	
	public static IHttpResponse sendHttpRequest(HttpRequestTypeEnum method,String url,String charSet) throws Exception{
		
		return sendHttpRequest(method,url,null,charSet);
	}
	
}

